//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwapIndex.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Instruments/VanillaSwap.h>
#include <gen/QL/Indexes/InterestRateIndex.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex) : CInterestRateIndex(CSwapIndex::typeid)
{
    CPeriod^ _Ctenor;
    CCurrency^ _Ccurrency;
    CCalendar^ _Ccalendar;
    CPeriod^ _CfixedLegTenor;
    CDayCounter^ _CfixedLegDayCounter;
    CIborIndex^ _CiborIndex;
    try
    {
#ifdef HANDLE
        _phSwapIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency _currency = static_cast<QuantLib::Currency> (_Ccurrency->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _CfixedLegTenor = safe_cast<CPeriod^> (fixedLegTenor);
        _CfixedLegTenor->Lock();
        QuantLib::Period& _fixedLegTenor = static_cast<QuantLib::Period&> (_CfixedLegTenor->GetReference ()); 
        QuantLib::BusinessDayConvention _fixedLegConvention = (QuantLib::BusinessDayConvention)fixedLegConvention ;
        _CfixedLegDayCounter = safe_cast<CDayCounter^> (fixedLegDayCounter);
        _CfixedLegDayCounter->Lock();
        QuantLib::DayCounter& _fixedLegDayCounter = static_cast<QuantLib::DayCounter&> (_CfixedLegDayCounter->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (new QuantLib::SwapIndex ( _familyName,  _tenor,  _settlementDays,  _currency,  _calendar,  _fixedLegTenor,  _fixedLegConvention,  _fixedLegDayCounter,  _iborIndex ));
        SetInterestRateIndex (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CfixedLegTenor != nullptr) _CfixedLegTenor->Unlock();
        if (_CfixedLegDayCounter != nullptr) _CfixedLegDayCounter->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
    }
}
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTermStructure) : CInterestRateIndex(CSwapIndex::typeid)
{
    CPeriod^ _Ctenor;
    CCurrency^ _Ccurrency;
    CCalendar^ _Ccalendar;
    CPeriod^ _CfixedLegTenor;
    CDayCounter^ _CfixedLegDayCounter;
    CIborIndex^ _CiborIndex;
    CYieldTermStructure^ _CdiscountingTermStructure;
    try
    {
#ifdef HANDLE
        _phSwapIndex = NULL;
#endif
        std::string _familyName = (std::string)ValueHelper::Convert (familyName); //d
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccurrency = safe_cast<CCurrency^> (currency);
        _Ccurrency->Lock();
        QuantLib::Currency _currency = static_cast<QuantLib::Currency> (_Ccurrency->GetReference ()); 
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _CfixedLegTenor = safe_cast<CPeriod^> (fixedLegTenor);
        _CfixedLegTenor->Lock();
        QuantLib::Period& _fixedLegTenor = static_cast<QuantLib::Period&> (_CfixedLegTenor->GetReference ()); 
        QuantLib::BusinessDayConvention _fixedLegConvention = (QuantLib::BusinessDayConvention)fixedLegConvention ;
        _CfixedLegDayCounter = safe_cast<CDayCounter^> (fixedLegDayCounter);
        _CfixedLegDayCounter->Lock();
        QuantLib::DayCounter& _fixedLegDayCounter = static_cast<QuantLib::DayCounter&> (_CfixedLegDayCounter->GetReference ()); 
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        _CdiscountingTermStructure = safe_cast<CYieldTermStructure^> (discountingTermStructure);
        _CdiscountingTermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountingTermStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountingTermStructure->GetHandle ()); 
        _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (new QuantLib::SwapIndex ( _familyName,  _tenor,  _settlementDays,  _currency,  _calendar,  _fixedLegTenor,  _fixedLegConvention,  _fixedLegDayCounter,  _iborIndex,  _discountingTermStructure ));
        SetInterestRateIndex (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ccurrency != nullptr) _Ccurrency->Unlock();
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CfixedLegTenor != nullptr) _CfixedLegTenor->Unlock();
        if (_CfixedLegDayCounter != nullptr) _CfixedLegDayCounter->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (_CdiscountingTermStructure != nullptr) _CdiscountingTermStructure->Unlock();
    }
}
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (boost::shared_ptr<QuantLib::SwapIndex>& childNative, Object^ owner) : CInterestRateIndex(CSwapIndex::typeid)
{
#ifdef HANDLE
	_phSwapIndex = NULL;
#endif
	_ppSwapIndex = &childNative;
    _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
}
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (QuantLib::SwapIndex& childNative, Object^ owner) : CInterestRateIndex(CSwapIndex::typeid)
{
#ifdef HANDLE
	_phSwapIndex = NULL;
#endif
	_ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (&childNative);
    _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
    _SwapIndexOwner = owner;
    _InterestRateIndexOwner = owner;
}

Cephei::QL::Indexes::CSwapIndex::CSwapIndex (CSwapIndex^ copy) : CInterestRateIndex(CSwapIndex::typeid)
{
#ifdef HANDLE
	_phSwapIndex = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (copy->GetShared());
        _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
    }
}
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (PLATFORM::Type^ t) : CInterestRateIndex(CSwapIndex::typeid)
{
#ifdef HANDLE
	_phSwapIndex = NULL;
#endif
	if (!t->IsSubclassOf(CSwapIndex::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (QuantLib::Handle<QuantLib::SwapIndex>& childNative, Object^ owner)  : CInterestRateIndex(CSwapIndex::typeid)
{
	_phSwapIndex = &childNative;
	_ppSwapIndex = &static_cast<boost::shared_ptr<QuantLib::SwapIndex>>(childNative.currentLink());
    _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
    _SwapIndexOwner = owner;
}
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (QuantLib::Handle<QuantLib::SwapIndex> childNative)  : CInterestRateIndex(CSwapIndex::typeid)
{
	_phSwapIndex = &childNative;
	_ppSwapIndex = &static_cast<boost::shared_ptr<QuantLib::SwapIndex>>(childNative.currentLink());
    _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::CSwapIndex::CSwapIndex (QuantLib::SwapIndex childNative)  : CInterestRateIndex(CSwapIndex::typeid)
{
#ifdef HANDLE
	_phSwapIndex = NULL;
#endif
	_ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (new QuantLib::SwapIndex (childNative));
    _ppInterestRateIndex = new boost::shared_ptr<QuantLib::InterestRateIndex> (boost::dynamic_pointer_cast<QuantLib::InterestRateIndex> (*_ppSwapIndex));
}
#endif

Cephei::QL::Indexes::CSwapIndex::~CSwapIndex ()
{
    if (_ppSwapIndex != NULL)
    {
	    delete _ppSwapIndex;
        _ppSwapIndex = NULL;
    }
}
Cephei::QL::Indexes::CSwapIndex::!CSwapIndex ()
{
    if (_ppSwapIndex != NULL)
    {
	    delete _ppSwapIndex;
    }
}
QuantLib::SwapIndex& Cephei::QL::Indexes::CSwapIndex::GetReference ()
{
    if (_ppSwapIndex == NULL) throw REFNEW NativeNullException ();
	return **_ppSwapIndex;
}
boost::shared_ptr<QuantLib::SwapIndex>& Cephei::QL::Indexes::CSwapIndex::GetShared ()
{
    if (_ppSwapIndex == NULL) throw REFNEW NativeNullException ();
	return *_ppSwapIndex;
}
QuantLib::SwapIndex* Cephei::QL::Indexes::CSwapIndex::GetPointer ()
{
    if (_ppSwapIndex == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwapIndex;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwapIndex>& Cephei::QL::Indexes::CSwapIndex::GetHandle ()
{
	if (_phSwapIndex == NULL)
	{
		_phSwapIndex = new Handle<QuantLib::SwapIndex> (*_ppSwapIndex);
	}
	return *_phSwapIndex;
}
#endif
bool Cephei::QL::Indexes::CSwapIndex::HasNative () 
{
	return (_ppSwapIndex != NULL);
}

Cephei::QL::Indexes::ISwapIndex^ Cephei::QL::Indexes::CSwapIndex::Clone (Cephei::QL::Termstructures::IYieldTermStructure^ forwarding)
{
    CYieldTermStructure^ _Cforwarding;
    try
    {
        _Cforwarding = safe_cast<CYieldTermStructure^> (forwarding);
        _Cforwarding->Lock();
        Handle<QuantLib::YieldTermStructure>& _forwarding = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cforwarding->GetHandle ()); 
    	boost::shared_ptr<QuantLib::SwapIndex> _rv = (boost::shared_ptr<QuantLib::SwapIndex>)(*_ppSwapIndex)->clone ( _forwarding );   
        Cephei::QL::Indexes::CSwapIndex^ _nrv = REFNEW Cephei::QL::Indexes::CSwapIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cforwarding != nullptr) _Cforwarding->Unlock();
    }
}
QL::Times::BusinessDayConventionEnum Cephei::QL::Indexes::CSwapIndex::FixedLegConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppSwapIndex)->fixedLegConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Indexes::CSwapIndex::FixedLegTenor::get ()
{
    try
    {
    	QuantLib::Period _rv = (QuantLib::Period)(*_ppSwapIndex)->fixedLegTenor ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IIborIndex^ Cephei::QL::Indexes::CSwapIndex::IborIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::IborIndex> _rv = (boost::shared_ptr<QuantLib::IborIndex>)(*_ppSwapIndex)->iborIndex ( );   
        Cephei::QL::Indexes::CIborIndex^ _nrv = REFNEW Cephei::QL::Indexes::CIborIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Indexes::CSwapIndex::MaturityDate (DateTime valueDate)
{
    try
    {
        QuantLib::Date _valueDate = (QuantLib::Date)ValueHelper::Convert (valueDate); //a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwapIndex)->maturityDate ( _valueDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::IVanillaSwap^ Cephei::QL::Indexes::CSwapIndex::UnderlyingSwap (DateTime fixingDate)
{
    try
    {
        QuantLib::Date _fixingDate = (QuantLib::Date)ValueHelper::Convert (fixingDate); //a
    	boost::shared_ptr<QuantLib::VanillaSwap> _rv = (boost::shared_ptr<QuantLib::VanillaSwap>)(*_ppSwapIndex)->underlyingSwap ( _fixingDate );   
        Cephei::QL::Instruments::CVanillaSwap^ _nrv = REFNEW Cephei::QL::Instruments::CVanillaSwap (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Indexes::CSwapIndex::ForwardingTermStructure::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppSwapIndex)->forwardingTermStructure ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Indexes::CSwapIndex::DiscountingTermStructure::get ()
{
    try
    {
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppSwapIndex)->discountingTermStructure ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = REFNEW Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Indexes::CSwapIndex::ExogenousDiscount::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSwapIndex)->exogenousDiscount ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::CSwapIndexVector::CSwapIndexVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Indexes::CSwapIndexVector::~CSwapIndexVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Indexes::CSwapIndexVector::!CSwapIndexVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Indexes::CSwapIndexVector::CSwapIndexVector (std::vector<QuantLib::SwapIndex>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Indexes::CSwapIndexVector::CSwapIndexVector (std::vector<QuantLib::SwapIndex> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::SwapIndex> (vec);
}
#endif
Cephei::QL::Indexes::CSwapIndexVector::CSwapIndexVector (std::vector<boost::shared_ptr<QuantLib::SwapIndex>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Indexes::CSwapIndexVector::CSwapIndexVector (std::vector<QuantLib::Handle<QuantLib::SwapIndex>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Indexes::CSwapIndexVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Indexes::ISwapIndex^ Cephei::QL::Indexes::CSwapIndexVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::SwapIndex& native = (*_pVValue)[index];
		return REFNEW CSwapIndex (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::SwapIndex& native = *(*_pVBoost)[index];
		return REFNEW CSwapIndex (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::SwapIndex& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CSwapIndex (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Indexes::CSwapIndexVector::default::set (int index, Cephei::QL::Indexes::ISwapIndex^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CSwapIndex^>(value)->GetReference ());
		}
        else
        {
            QuantLib::SwapIndex& c = (*_pVValue)[index];
            c = dynamic_cast<CSwapIndex^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CSwapIndex^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::SwapIndex>& c = (*_pVBoost)[index];
		c = dynamic_cast<CSwapIndex^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::SwapIndex> ());
		}
		QuantLib::Handle<QuantLib::SwapIndex>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::SwapIndex>& b = static_cast<boost::shared_ptr<QuantLib::SwapIndex>>(c.currentLink());
		b = dynamic_cast<CSwapIndex^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^ Cephei::QL::Indexes::CSwapIndexVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<ISwapIndex^> (dynamic_cast<ISwapIndex^>(default[index]));
}

void Cephei::QL::Indexes::CSwapIndexVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^ value) 
{
	default[index] = safe_cast<ISwapIndex^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::SwapIndex>& Cephei::QL::Indexes::CSwapIndexVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::SwapIndex>>& Cephei::QL::Indexes::CSwapIndexVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::SwapIndex>>& Cephei::QL::Indexes::CSwapIndexVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Indexes::CSwapIndexVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::SwapIndex> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::SwapIndex>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::SwapIndex>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::ISwapIndex^ Cephei::QL::Indexes::CSwapIndex_Factory::Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex)
{
    return REFNEW CSwapIndex ( familyName,  tenor,  settlementDays,  currency,  calendar,  fixedLegTenor,  fixedLegConvention,  fixedLegDayCounter,  iborIndex);
}
Cephei::QL::Indexes::ISwapIndex^ Cephei::QL::Indexes::CSwapIndex_Factory::Create (String^ familyName, Cephei::QL::Times::IPeriod^ tenor, UInt32 settlementDays, Cephei::QL::ICurrency^ currency, Cephei::QL::Times::ICalendar^ calendar, Cephei::QL::Times::IPeriod^ fixedLegTenor, QL::Times::BusinessDayConventionEnum fixedLegConvention, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTermStructure)
{
    return REFNEW CSwapIndex ( familyName,  tenor,  settlementDays,  currency,  calendar,  fixedLegTenor,  fixedLegConvention,  fixedLegDayCounter,  iborIndex,  discountingTermStructure);
}
/* <summary>Create a single dimension array of SwapIndex that can be passed into other function </summary> */
IVector<Cephei::QL::Indexes::ISwapIndex^>^ Cephei::QL::Indexes::CSwapIndex_Factory::CreateVector ()
 {
	 return REFNEW CoVector<ISwapIndex^> (REFNEW CSwapIndexVector ());
 }
IVector<Cephei::QL::Indexes::ISwapIndex^>^ Cephei::QL::Indexes::CSwapIndex_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<ISwapIndex^>^ source)
 {
	 return REFNEW CoVector<ISwapIndex^> (source, REFNEW CSwapIndexVector ());
 }
/* <summary>Create a single dimension array of SwapIndex with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^>^>^ Cephei::QL::Indexes::CSwapIndex_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^>^> (REFNEW CoCellVector<Cephei::QL::Indexes::ISwapIndex^> (REFNEW CSwapIndexVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^>^>^ Cephei::QL::Indexes::CSwapIndex_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Indexes::ISwapIndex^>^>^> (REFNEW CoCellVector<Cephei::QL::Indexes::ISwapIndex^> (source, REFNEW CSwapIndexVector ()));
 }

